//
//  CommonResigstViewController.m
//  Tang
//
//  Created by 邓芮 on 15/12/10.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "CommonResigstViewController.h"

@interface CommonResigstViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *password1;

@end

@implementation CommonResigstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

}


- (IBAction)success:(id)sender
{
    BmobUser *bUser = [[BmobUser alloc]init];
//    [bUser setObject:[UIImage imageNamed:@"happy"] forKey:@"headerImage"];
//    [bUser setObject:[UIImage imageNamed:@"background"] forKey:@"backgroundImage"];
    if (self.userName.text.length == 0)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            [UIView animateWithDuration:1 animations:^{
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }];
        return;
    }
    else
    {
        [bUser setUsername:self.userName.text];
    }
    
    if (self.password.text.length < 6)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"密码不能小于6位" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            [UIView animateWithDuration:1 animations:^{
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }];
        return;
    }
    else if(![self.password.text isEqualToString:self.password1.text])
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"两次输入错误" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            [UIView animateWithDuration:1 animations:^{
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }];
        return;
    }
    else
    {
        [bUser setPassword:self.password.text];
    }
    
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful)
        {
            [BmobUser logout];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"该用户名已经存在" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                [UIView animateWithDuration:1 animations:^{
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
            }];
            return;
        }
    }];
   
}

- (IBAction)dissmiss:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.userName resignFirstResponder];
    [self.password resignFirstResponder];
    [self.password1 resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
